import myMath from '../../../public/js/myservice/common/my-math'
import { settlementType } from '../constant'

export default ({
    oldProd,
    $scope,
}) => ["$scope", "modalInstance", "$timeout", "isSpecial", ($dialogScope, modalInstance, $timeout, isSpecial) => {
    $dialogScope.oldProd = oldProd;
    $dialogScope.data = oldProd || {};
    //编辑情况下 明源签约-直属  明源签约-联营 联营签约 - 云客结算 不可以修改某些字段
    $dialogScope.cannotEdit = isSpecial($scope.contractBaseInfo.contract_property_id) && !!oldProd;
    $dialogScope.$watch('data.authorization_numeric', function (itemVal) {
        if (+$dialogScope.data.authorization_type === 0 && $dialogScope.data.unique_name !== "ad") {
            const period = $dialogScope.data.rent_period || 0;
            $dialogScope.data.amount = Math.round(myMath.div(myMath.mul(myMath.mul(myMath.mul(itemVal, period), 1), $dialogScope.data.price), 12));
        }
    });
    $dialogScope.$watch('data.rent_period', function (itemVal) {
        if (+$dialogScope.data.authorization_type === 0 && $dialogScope.data.unique_name !== "ad") {
            const number = $dialogScope.data.authorization_numeric || 0;
            $dialogScope.data.amount = Math.round(myMath.div(myMath.mul(myMath.mul(myMath.mul(itemVal, number), 1), $dialogScope.data.price), 12));
        }
    });
    $dialogScope.$watch('data.price', function (itemVal) {
        if (parseFloat(itemVal) == 0) {
            $dialogScope.data.amount = 0;
        }
        if ($dialogScope.data.application_id === "other") {
            const number = $dialogScope.data.authorization_numeric || 0;
            const period = $dialogScope.data.rent_period || 0;
            $dialogScope.data.amount = Math.round(myMath.div(myMath.mul(myMath.mul(myMath.mul(number, period), 1), itemVal), 12));
        }
    });

    //签约分摊金额=原签约分摊金额-签约调整金额
    $dialogScope.$watch('data.adjust_settlement_average_amount', itemVal => {
        if (itemVal !== undefined) {
            const origin = $dialogScope.data.origin_settlement_average_amount || 0;
            $dialogScope.data.settlement_average_amount = myMath.sub(origin - itemVal);
        }
    });
    $dialogScope.$watch('data.origin_settlement_average_amount', itemVal => {
        if (itemVal !== undefined) {
            const adjust = $dialogScope.data.adjust_settlement_average_amount || 0;
            $dialogScope.data.settlement_average_amount = myMath.sub(itemVal - adjust);
        }
    });
    //签约分摊金额：原签约分摊金额-签约调整金额
    $dialogScope.$watch('data.adjust_sign_average_amount', itemVal => {
        if (itemVal !== undefined) {
            const origin = $dialogScope.data.origin_sign_average_amount || 0;
            $dialogScope.data.sign_average_amount = myMath.sub(origin - itemVal);
        }
    });
    $dialogScope.$watch('data.origin_sign_average_amount', itemVal => {
        if (itemVal !== undefined) {
            const adjust = $dialogScope.data.adjust_sign_average_amount || 0;
            $dialogScope.data.sign_average_amount = myMath.sub(itemVal - adjust);
        }
    });
    //产品租金分摊金额：原产品租金分摊金额-产品租金调整金额
    $dialogScope.$watch('data.adjust_product_average_amount', itemVal => {
        if (itemVal !== undefined) {
            const origin = $dialogScope.data.origin_product_average_amount || 0;
            $dialogScope.data.product_average_amount = myMath.sub(origin - itemVal);
        }
    });
    $dialogScope.$watch('data.origin_product_average_amount', itemVal => {
        if (itemVal !== undefined) {
            const adjust = $dialogScope.data.adjust_product_average_amount || 0;
            $dialogScope.data.product_average_amount = myMath.sub(itemVal - adjust);
        }
    });
    //标准金额变更之后，相关联的数据也要变更
    $dialogScope.$watch('data.amount', itemVal => {
        if (itemVal !== undefined && oldProd == undefined) {
            //产品总额
            let allAmount = 0;
            $.each($scope.projects, function (i, d) {
                allAmount = myMath.add(allAmount, d.amount);
            });
            allAmount = myMath.add(allAmount, itemVal);
            //原结算分摊金额
            const balanceAmount = $scope.balanceInfo.balance_amount;
            if (allAmount != 0) {
                $dialogScope.data.origin_settlement_average_amount = Math.round(myMath.mul(myMath.div(itemVal, allAmount), balanceAmount));
            }
            //原签约分摊金额
            const signAmount = $scope.contractBaseInfo.sign_amount;
            if (allAmount != 0) {
                $dialogScope.data.origin_sign_average_amount = Math.round(myMath.mul(myMath.div(itemVal, allAmount), signAmount));
            }
            //原产品租金分摊金额
            const rentAmount = $scope.contractBaseInfo.rent_amount;
            if (allAmount != 0) {
                $dialogScope.data.origin_product_average_amount = Math.round(myMath.mul(myMath.div(itemVal, allAmount), rentAmount));
            }
        }
    });
    $dialogScope.ddlConfigProjects = {
        allowClear: false,
        data: $scope.ddlConfigProjectsData,
        placeholder: '产品名称',
        onchange2(item) {
            $dialogScope.data.authorization_type = item.authorization_type;
            $dialogScope.data.application_name = item.application_name;
            $dialogScope.data.application_id = item.application_id;
            $dialogScope.data.unique_name = item.unique_name;

            if (item.authorization_type == 0 && item.applicationPrices.length) {
                $dialogScope.data.price = item.applicationPrices[0].price;
                const period = $dialogScope.data.rent_period || 0;
                $dialogScope.data.amount = Math.round(myMath.div(myMath.mul(myMath.mul($dialogScope.data.authorization_numeric, period), $dialogScope.data.price), 12));
            }

            const appPrices = [];
            $.each(item.applicationPrices || [], function (i, d) {
                appPrices.push({ id: d.price, text: d.unit });
            });

            $dialogScope.ddlConfigSMSPrice = {
                allowClear: false,
                data: appPrices,
                placeholder: '标准单价',
                onchange2(price) {
                    $dialogScope.data.unit = price.text;
                }
            };

            //合同属性
            const contractTypeData = [
                { "id": 1, "text": "新增" },
                { "id": 2, "text": "增购" },
                { "id": 3, "text": "续签" }
            ];
            if ($dialogScope.data.contract_type != undefined && $dialogScope.data.settlement_scale == undefined) {
                if ($dialogScope.data.contract_type == 3) {
                    $dialogScope.data.settlement_scale = .50;
                } else {
                    $dialogScope.data.settlement_scale = .35;
                }
            }

            const bindDdlConfigContractType = () => {
                $dialogScope.ddlConfigContractType = {
                    allowClear: false,
                    data: contractTypeData,
                    placeholder: '合同属性',
                    onchange2(contractType) {
                        $dialogScope.data.contract_type = contractType.id;
                        if (contractType.id == 3) {
                            $dialogScope.data.settlement_scale = 0.50;
                        } else {
                            $dialogScope.data.settlement_scale = 0.35;
                        }
                    }
                };
                $dialogScope.$applyAsync();
            };

            const bind = isAsync => {
                if (isAsync) {
                    $timeout(bindDdlConfigContractType, 1);
                } else {
                    bindDdlConfigContractType();
                }
            };

            bind($dialogScope.data.settlement_scale !== undefined);

            $dialogScope.ddlConfigSaas = {
                allowClear: false,
                data: $scope.ddlConfigSaas.data,
                placeholder: '合同属性',
                onchange2(isSaas) {
                    $dialogScope.data.is_saas = isSaas.id;
                }
            };

            //结算类型
            $dialogScope.ddlConfigSettlementType = {
                allowClear: false,
                data: settlementType,
                placeholder: '结算类型',
                onchange2(_settlementType) {
                    $dialogScope.data.settlement_type = _settlementType.id;
                }
            };
            $dialogScope.$applyAsync();
        }
    };
    //产品明细
    $dialogScope.ddlConfigOtherProjects = {
        allowClear: false,
        data: $scope.ddlConfigOtherProjectsData,
        placeholder: '其他产品',
        onchange2(item) {
            $dialogScope.data.other_application_name = item.application_name;
            $dialogScope.data.other_application_id = item.application_id
        }
    };

    if (oldProd === undefined) {
        //合同属性
        const attributeNameArr = $scope.contractBaseInfo.attribute_name.split(',');
        const isXz = attributeNameArr[0] == "true";
        const isHq = attributeNameArr[1] == "true";
        const isXq = attributeNameArr[2] == "true";
        let isZg = false;
        if (attributeNameArr.length == 4) {
            isZg = attributeNameArr[3] == "true";
        }
        if (isXz && !isHq && !isXq && !isZg) {
            $dialogScope.data.contract_type = 1;
        }
        if (!isXz && !isHq && !isXq && isZg) {
            $dialogScope.data.contract_type = 2;
        }
        if (!isXz && !isHq && isXq && !isZg) {
            $dialogScope.data.contract_type = 3;
        }
    }
    $dialogScope.ok = function () {
        modalInstance.resolve($dialogScope.data);
    }
    $dialogScope.cancel = function () {
        modalInstance.reject();
    }
}]